import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react-swc';
import path from "path";
import svgr from 'vite-plugin-svgr';
import tailwindcss from 'tailwindcss'
import autoprefixer from 'autoprefixer'
// https://vitejs.dev/config/

export default defineConfig({
    optimizeDeps: {
        include: ['bpmn-js-properties-panel', '@bpmn-io/properties-panel']
    },
    plugins: [react(), svgr({ svgrOptions: { icon: true } })],
    server: {
        port: 90, // 自定义端口号
        host: true,
        open: true,
        proxy: {
            // https://cn.vitejs.dev/config/#server-proxy
            '/prod-api': {
                target: 'http://localhost:8099',
                changeOrigin: true,
                rewrite: (p) => p.replace(/^\/prod-api/, '')
            }
        }
    },

    resolve: {
        alias: {
            "@": path.resolve(__dirname, './src')
        }
    },
    build: {
        outDir: 'dist', // 默认输出目录
        assetsDir: 'assets', // 静态资源目录
        rollupOptions: {
            output: {
                // JS 文件
                chunkFileNames: 'assets/js/[name]-[hash].js',
                entryFileNames: 'assets/js/[name]-[hash].js',

                // CSS 文件（通过插件处理）
                assetFileNames: (assetInfo) => {
                    if (assetInfo.type === 'asset') {
                        const ext = assetInfo.name?.split('.').pop() || '';

                        if (/png|jpe?g|svg|gif|tiff|bmp|ico/i.test(ext)) {
                            return 'img/[name]-[hash][extname]';
                        }
                        if (/woff2?|ttf|otf|eot/i.test(ext)) {
                            return 'fonts/[name]-[hash][extname]';
                        }
                        if (/css/i.test(ext)) {
                            return 'css/[name]-[hash][extname]';
                        }
                    }
                    return 'assets/[name]-[hash][extname]';
                }
            },
        },
    },
    assetsInclude: ['**/*.JPG'],// 添加这一行
    css: {
        postcss: {
            plugins: [
                tailwindcss,
                autoprefixer
            ]
        }
    },
})
